<?php

namespace app\admin\controller;

use app\admin\lib\exception\BaseException;
use app\admin\service\AdminService;
use app\admin\service\AvatarService;
use app\admin\validate\Common as CommonValidate;
use app\BaseController;

class Admin extends BaseController
{
    protected $adminService;
    protected $avatarService;

    protected function initialize()
    {
        parent::initialize();
        $this->adminService = new AdminService();
        $this->avatarService = new AvatarService();
    }

    /**
     * 获取管理员列表
     */
    public function list()
    {
        try {
            $data = $this->request->only(['limit', 'page'], 'get');

            $data = array_merge([
                'page' => 1,
                'limit' => 10
            ], $data);

            validate(CommonValidate::class)
                ->scene('list')
                ->check($data);

            $result = $this->adminService->getAdminList((int)$data['page'], (int)$data['limit']);

            return json([
                'code' => 200,
                'message' => '查询成功',
                'data' => $result
            ]);
        } catch (\Exception $e) {
            throw new BaseException([
                'code' => 400,
                'message' => $e->getMessage(),
                'errorCode' => 10001,
            ]);
        }
    }

    /**
     * 添加管理员
     */
    public function add()
    {
        $data = $this->request->only([
            'username', 'password', 'email', 'phone', 'role', 'status', 'avatar'
        ], 'post');

        try {
            // 验证数据
            validate(CommonValidate::class)->scene('add')->check($data);

            // 调用service添加数据
            $adminData = $this->adminService->addAdmin($data);

            return json([
                'code' => 200,
                'message' => '添加成功',
                'data' => $adminData
            ]);
        } catch (\Exception $e) {
            throw new BaseException([
                'code' => 400,
                'message' => $e->getMessage(),
                'errorCode' => 60001
            ]);
        }
    }

    /**
     * 删除管理员
     */
    public function delete()
    {
        try {
            $id = $this->request->param('id');

            validate(CommonValidate::class)->scene('delete')->check(['id' => $id]);

            $operatorRole = $this->request->user['role'];

            $this->adminService->deleteAdmin((int)$id, (int)$operatorRole);

            return json([
                'code' => 200,
                'message' => '删除管理员成功',
                'data' => []
            ]);
        } catch (\Exception $e) {
            throw new BaseException([
                'code' => 400,
                'message' => $e->getMessage(),
                'errorCode' => 60000
            ]);
        }
    }

    /**
     * 编辑管理员
     */
    public function edit()
    {
        $operatorRole = $this->request->user['role'];
        $avatarFile = $this->request->file('avatar');
        $data = $this->request->only([
            'id', 'username', 'password', 'email', 'phone', 'role', 'status'
        ], 'post');

        try {
            // 处理头像上传
            if ($avatarFile) {
                $data['avatar'] = $this->avatarService->uploadAvatar($avatarFile, (int)$data['id']);
            }

            validate(CommonValidate::class)->scene('edit')->check($data);

            $result = $this->adminService->editAdmin($data, (int)$operatorRole);

            return json([
                'code' => 200,
                'message' => '更新成功',
                'data' => $result
            ]);
        } catch (\Exception $e) {
            throw new BaseException([
                'code' => 400,
                'message' => $e->getMessage(),
                'errorCode' => 60005
            ]);
        }
    }

}
